package com.lckj.android.shell;

import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.webkit.WebView;

import androidx.annotation.Nullable;

import java.util.List;

/**
 * @author lc. 2023-10-22 15:24
 */
public class App extends Application {
    private static volatile App sInstance = null;
    @Override
    public void onCreate() {
        super.onCreate();
        sInstance = this;
        String processName = getProcessName(this, android.os.Process.myPid());
        if (processName != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                WebView.setDataDirectorySuffix(processName);
            }
            boolean defaultProcess = processName.equals(getPackageName());
            if (defaultProcess) {
                initApp();
            }
        }
    }

    public static App getInstance() {
        return sInstance;
    }

    private void initApp() {

    }


    /*** 获取进程名 */
    @Nullable
    private String getProcessName(Context cxt, int pid) {
        ActivityManager am = (ActivityManager) cxt.getSystemService(Context.ACTIVITY_SERVICE);
        if (am == null) {
            return null;
        }
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps == null) {
            return null;
        }
        for (ActivityManager.RunningAppProcessInfo processInfo : runningApps) {
            if (processInfo.pid == pid) {
                return processInfo.processName;
            }
        }
        return null;
    }
}
